package com.spring.housing1.deliver;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.spring.housing1.util.HttpRequestUtil;
import okhttp3.*;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Base64;

/**
* 图像增强的api
* */
public class PitureEnhance {

    //
    public static String url = "https://aip.baidubce.com/rest/2.0/image-process/v1/image_definition_enhance";
    public static String appId = "31033796";
    public static String apiKey = "wYcDjQLNOORZQCYxklh7Ci3x";
    public static String secretKey = "QM2zFdYeqeWEAgkaphcljDo9SPqkf2eH";
    //    获取access_token url
    public static String tokenUrl = "https://aip.baidubce.com/oauth/2.0/token?client_id=" + apiKey + "&client_secret=" + secretKey + "&grant_type=client_credentials";


    public static String getAccessToken() {
        String s = HttpRequestUtil.postRequest(tokenUrl, null);
        JSONObject jsonObject = JSONUtil.parseObj(s);
        jsonObject.get("access_token");
        return jsonObject.get("access_token").toString();
    }

//    获取对比度增强后的图片
    public static InputStream getImage(byte[] files) throws Exception {
        String access_token =  getAccessToken();
        String fileContentAsBase64Urlencoded = getFileContentAsBase64Urlencoded(files);

        OkHttpClient HTTP_CLIENT = new OkHttpClient().newBuilder().build();
        MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
        // image 可以通过 getFileContentAsBase64("C:\fakepath\czzw.png") 方法获取,如果Content-Type是application/x-www-form-urlencoded时,使用getFileContentAsBase64Urlencoded方法获取
        RequestBody body = RequestBody.create(mediaType, "image="+fileContentAsBase64Urlencoded);
        Request request = new Request.Builder()
                .url("https://aip.baidubce.com/rest/2.0/image-process/v1/contrast_enhance?access_token="+access_token)
                .method("POST", body)
                .addHeader("Content-Type", "application/x-www-form-urlencoded")
                .addHeader("Accept", "application/json")
                .build();
        Response response = HTTP_CLIENT.newCall(request).execute();
        String string = response.body().string();
        String iamge = JSONUtil.parseObj(string).get("image").toString();
        byte[] bytes = Base64.getDecoder().decode(iamge.toString());
       ByteArrayInputStream stream = new ByteArrayInputStream(bytes);
       return  stream;
    }

    //    获取清晰度增强后的图片
    public static InputStream getImage2(byte[] files) throws Exception {
        String access_token =  getAccessToken();
        String fileContentAsBase64Urlencoded = getFileContentAsBase64Urlencoded(files);
        OkHttpClient HTTP_CLIENT = new OkHttpClient().newBuilder().build();
        MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
        // image 可以通过 getFileContentAsBase64("C:\fakepath\czzw.png") 方法获取,如果Content-Type是application/x-www-form-urlencoded时,使用getFileContentAsBase64Urlencoded方法获取
        RequestBody body = RequestBody.create(mediaType, "image="+fileContentAsBase64Urlencoded);
        Request request = new Request.Builder()
                .url("https://aip.baidubce.com/rest/2.0/image-process/v1/image_definition_enhance?access_token="+access_token)
                .method("POST", body)
                .addHeader("Content-Type", "application/x-www-form-urlencoded")
                .addHeader("Accept", "application/json")
                .build();
        Response response = HTTP_CLIENT.newCall(request).execute();
        String string = response.body().string();
        String iamge = JSONUtil.parseObj(string).get("image").toString();
        byte[] bytes = Base64.getDecoder().decode(iamge.toString());
        ByteArrayInputStream stream = new ByteArrayInputStream(bytes);
        return  stream;
    }






   /** 获取文件base64编码
     * @param path 文件路径
     * @return base64编码信息，不带文件头
     * @throws IOException IO异常
     */
    static String getFileContentAsBase64(byte[] files) throws IOException {
        return Base64.getEncoder().encodeToString(files);
    }
    /**
     * 获取文件base64 UrlEncode编码
     * @param path 文件路径
     * @return base64编码信息，不带文件头
     * @throws IOException IO异常
     */
    static String getFileContentAsBase64Urlencoded(byte[] files) throws IOException {
        return URLEncoder.encode(getFileContentAsBase64(files), "utf-8");
    }

    public static void main(String[] args) {
        String accessToken = getAccessToken();
        System.out.print(accessToken);
    }
}
